import java.util.Enumeration;
import java.util.Hashtable;

public class Directory extends SystemItem {

	private Hashtable<String, SystemItem> contents;

	// Initialize a directory object with an empty hashtable to hold
	// the files and directories it contains.
	public Directory(String n, Directory d) {
		super(n, d);
		contents = new Hashtable<String, SystemItem>();
		try {
			addPeriods();
		} catch (DuplicateItemException e) {
			e.printStackTrace();
		}
	}

	public void addPeriods() throws DuplicateItemException {
		addItem(".", this);
		if (this.getParent() != null) {
			addItem("..", this.getParent());
		} else {
			addItem("..", this);
		}

	}

	public void addItem(SystemItem item) throws DuplicateItemException {
		this.addItem(item.getName(), item);
	}

	public void addItem(String n, SystemItem item)
			throws DuplicateItemException {
		if (contents.containsKey(n))
			throw new DuplicateItemException(
					"Error: Item with same name exists in directory.");
		contents.put(n, item);
	}

	// Remove an item from directory's contents, with the key 'key'.
	public void removeItem(String key) {
		contents.remove(key);
	}

	public Hashtable<String, SystemItem> getContents() {
		return contents;
	}

	public void printContents() {
		Enumeration<String> keys = contents.keys();
		while (keys.hasMoreElements()) {
			String itemname = keys.nextElement();
			if (itemname != "." && itemname != "..")
				System.out.println(itemname);
		}
	}
}
